/****************************************************************************\
 * @file    game_fuc.c
 * @author  马新硕
 * @date    2022/09/20
 * @brief   游戏的主要执行函数
 * @note    碰撞判断、地图更新等等
 ******************************************************************************
 * @attent  
 ****************************************************************************/
 
#include "game_fuc.h"

extern osMessageQueueId_t UsersbulletQueueHandle;//消息队列句柄

uint16_t mapBoundary_Up, mapBoundary_Down, mapBoundary_Left, mapBoundary_Right;//地图边界

UsersRole_t *usersrole;//玩家角色
Weapon_t *usersweapon;//玩家武器
uint8_t weaponType = 0;//武器种类    0枪     1近战武器
  
Enemy_t enemies[ENEMIES_MAX];//敌人
Bullet_t usersbullet[USERSBULLET_MAX];//玩家子弹
Bullet_t usersattack;                 //玩家近战波
Bullet_t enemiesbullet[ENEMIESBULLET_MAX];//敌人子弹

static uint8_t BossFireFlag=0;
static uint8_t weak_flag=0;
static uint8_t strong_flag=0;

Barrier_t mapBarrier[MAPBARRIER_MAX];//地图不可破坏障碍物
DestructibleBarrier_t mapDesBarrier[MAPBARRIER_MAX];//地图可破坏障碍物

/****************初始化游戏****************/
void game_Init(void)
{
  ILI9341_Init();
  LCD_SetColors(RED,WHITE);
  ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);//清屏
  Joystick_Init();
  
  mapBoundary_Left = Wall.Pixel_x;
  mapBoundary_Right = LCD_X_LENGTH-1-Wall.Pixel_x;
  mapBoundary_Up = StatusBar1.Pixel_y+Wall.SquarePixel_y;
  mapBoundary_Down = LCD_Y_LENGTH-1-Wall.Pixel_y-8;
  
  usersrole = &Knight;
  usersweapon = &LittleGun;

  enemies[0] = Enemy1;
  enemies[1] = Enemy2;
}


/****************玩家和怪物操作****************/
//玩家操作
void usersControl(void)
{
  static uint8_t run_cnt = 0;//角色跑动切换图片计数
  
  static uint8_t usersSkillFlag = 0;//角色使用技能标志
  static uint8_t usersSkillcnt = 0, usersPowercnt = 0;//角色技能相关计数
  
  float currentVel_x, currentVel_y;//角色当前速度
  
  static uint8_t switchWeaponFlag = 0;//武器种类标志
  static uint8_t switchWeaponcnt = 0;//武器切换计数标志
  
  if(usersrole == &Knight)
  {
    //判断玩家是否死亡
    if(usersrole->Health == 0)
    {
      if(usersrole->lastVel_x >= 0.0f){usersrole->pPic = knightRunPic.pDead;}
      else{usersrole->pPic = knightRunPic.pDead_r;}
    }
    else//没有死亡
    {
      //玩家移动
      currentVel_x = (float)(JoyStick.JoyX/sqrt(JoyStick.JoyX*JoyStick.JoyX + JoyStick.JoyY*JoyStick.JoyY)) * 6;
      currentVel_y = (float)(JoyStick.JoyY/sqrt(JoyStick.JoyX*JoyStick.JoyX + JoyStick.JoyY*JoyStick.JoyY)) * 6;
      
      if(abs(JoyStick.JoyX)<=20 && abs(JoyStick.JoyY)<=20)//原地不动
      {
        if(usersrole->lastVel_x >= 0.0f)
        {
          usersrole->pPic = knightRunPic.pStand1;
          //更新武器图片和位置
          if(weaponType==1&&JoyStick.ButtonD==1){usersweapon = &Knife_2;usersweapon->pPic = Weapon_Pic.pKnife2;
              usersweapon->wVertec_x = usersrole->Center_x ;
              usersweapon->wVertec_y = usersrole->Center_y -10;
              usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
              usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==1&&JoyStick.ButtonD==0){usersweapon = &Knife;usersweapon->pPic = Weapon_Pic.pKnife;
              usersweapon->wVertec_x = usersrole->Center_x;
              usersweapon->wVertec_y = usersrole->Center_y;
              usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
              usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==0){usersweapon = &LittleGun;usersweapon->pPic = Weapon_Pic.pLittleGun;
              usersweapon->wVertec_x = usersrole->Center_x;
              usersweapon->wVertec_y = usersrole->Center_y;
              usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
              usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}

        }
        else
        {
          usersrole->pPic = knightRunPic.pStand1_r;
          if(weaponType==1&&JoyStick.ButtonD==1){usersweapon = &Knife_2;usersweapon->pPic = Weapon_Pic.pKnife2_r;
            usersweapon->wVertec_x = usersrole->Center_x - usersweapon->wPixel_x ;
            usersweapon->wVertec_y = usersrole->Center_y -10;
            usersweapon->wGunpoint_x = usersweapon->wVertec_x;
            usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==1&&JoyStick.ButtonD==0){usersweapon = &Knife;usersweapon->pPic = Weapon_Pic.pKnife_r;
            usersweapon->wVertec_x = usersrole->Center_x - usersweapon->wPixel_x;
            usersweapon->wVertec_y = usersrole->Center_y;
            usersweapon->wGunpoint_x = usersweapon->wVertec_x;
            usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==0){usersweapon = &LittleGun;usersweapon->pPic = Weapon_Pic.pLittleGun_r;
            usersweapon->wVertec_x = usersrole->Center_x - usersweapon->wPixel_x;
            usersweapon->wVertec_y = usersrole->Center_y;
            usersweapon->wGunpoint_x = usersweapon->wVertec_x;
            usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}

        }
        run_cnt = 0;
        usersrole->Vel_x = 0.0f;
        usersrole->Vel_y = 0.0f;
        usersrole->lastCenter_x = usersrole->Center_x;//记录上一帧的位置
        usersrole->lastCenter_y = usersrole->Center_y;
      }
      else//在移动
      {
        switch(run_cnt)
        {
          case 0:
            if(currentVel_x >= 0.0f){usersrole->pPic = knightRunPic.pRun1;}
            else{usersrole->pPic = knightRunPic.pRun1_r;}
            run_cnt++;
            break;
          
          case 2:
            if(currentVel_x >= 0.0f){usersrole->pPic = knightRunPic.pRun2;}
            else{usersrole->pPic = knightRunPic.pRun2_r;}
            run_cnt++;
            break;
          
          case 4:
            if(currentVel_x >= 0.0f){usersrole->pPic = knightRunPic.pRun3;}
            else{usersrole->pPic = knightRunPic.pRun3_r;}
            run_cnt++;
            break;
          
          case 6:
            if(currentVel_x >= 0.0f){usersrole->pPic = knightRunPic.pRun4;}
            else{usersrole->pPic = knightRunPic.pRun4_r;}
            run_cnt = 0;
            break;
          
          default:
            run_cnt++;
            break;
        }
        usersrole->Vel_x = currentVel_x;
        usersrole->Vel_y = currentVel_y;
        usersrole->lastVel_x = currentVel_x;
        usersrole->lastVel_y = currentVel_y;
        
        usersrole->lastCenter_x = usersrole->Center_x;//记录上一帧的位置
        usersrole->lastCenter_y = usersrole->Center_y;
        usersrole->Center_x += usersrole->Vel_x;//更新这一帧的位置
        usersrole->Vertec_x += usersrole->Vel_x;
        usersrole->Center_y += usersrole->Vel_y;
        usersrole->Vertec_y += usersrole->Vel_y;
        
        //更新武器图片和位置
        if(currentVel_x >= 0.0f)
        {
          if(weaponType==1&&JoyStick.ButtonD==1){usersweapon = &Knife_2;usersweapon->pPic = Weapon_Pic.pKnife2;
            usersweapon->wVertec_x = usersrole->Center_x ;
            usersweapon->wVertec_y = usersrole->Center_y- 10;
            usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
            usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==1&&JoyStick.ButtonD==0){usersweapon = &Knife;usersweapon->pPic = Weapon_Pic.pKnife;
            usersweapon->wVertec_x = usersrole->Center_x;
            usersweapon->wVertec_y = usersrole->Center_y;
            usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
            usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==0){usersweapon = &LittleGun;usersweapon->pPic = Weapon_Pic.pLittleGun;
            usersweapon->wVertec_x = usersrole->Center_x;
            usersweapon->wVertec_y = usersrole->Center_y;
            usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
            usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}

        }
        else
        {
          if(weaponType==1&&JoyStick.ButtonD==1){usersweapon = &Knife_2;usersweapon->pPic = Weapon_Pic.pKnife2_r;
              usersweapon->wVertec_x = usersrole->Center_x - usersweapon->wPixel_x ;
              usersweapon->wVertec_y = usersrole->Center_y -10;
              usersweapon->wGunpoint_x = usersweapon->wVertec_x;
              usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==1&&JoyStick.ButtonD==0){usersweapon = &Knife;usersweapon->pPic = Weapon_Pic.pKnife_r;
              usersweapon->wVertec_x = usersrole->Center_x - usersweapon->wPixel_x;
              usersweapon->wVertec_y = usersrole->Center_y;
              usersweapon->wGunpoint_x = usersweapon->wVertec_x;
              usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}
          if(weaponType==0){usersweapon = &LittleGun;usersweapon->pPic = Weapon_Pic.pLittleGun_r;
              usersweapon->wVertec_x = usersrole->Center_x - usersweapon->wPixel_x;
              usersweapon->wVertec_y = usersrole->Center_y;
              usersweapon->wGunpoint_x = usersweapon->wVertec_x;
              usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;}

        }
      }
      
      //玩家射击
      usersShoot();
      
      //玩家技能
      if(JoyStick.ButtonB==1 && usersrole->Power==usersrole->MaxPower)//使用技能
      {
        usersweapon->wGenTime /= 2;
        usersSkillFlag = 1;
      }
      if(usersSkillFlag)//技能条减少(正在使用技能)
      {
        if(usersSkillcnt%1 == 0){usersrole->Power--;}
        usersSkillcnt++;
        if(usersSkillcnt >= StatusBar3.DrawPiexl_x*1){usersSkillcnt = 0; usersSkillFlag = 0; usersweapon->wGenTime *= 2;}
      }
      if(usersrole->Power<usersrole->MaxPower && usersSkillFlag==0)//技能条增加(进入CD)
      {
        if(usersPowercnt%2 == 0){usersrole->Power++;}
        usersPowercnt++;
        if(usersPowercnt >= StatusBar4.DrawPiexl_x*2){usersPowercnt = 0;}
      }
      
      //切换武器
      if(JoyStick.ButtonC==1 && switchWeaponFlag==1)
      {
        if(weaponType == 0){usersweapon = &Knife; weaponType = 1; switchWeaponFlag = 0;}
        else{usersweapon = &LittleGun; weaponType = 0; switchWeaponFlag = 0;}
      }
      if(switchWeaponFlag==0 && switchWeaponcnt<10){switchWeaponcnt++;}
      else if(switchWeaponcnt>=10){switchWeaponFlag = 1; switchWeaponcnt = 0;}
      
      //近战武器操作
//      usersAttack();
    }
  }
  else//TODO 加入其他角色
  {

  }
}
//void usersAttack(void)
//{
//  static uint8_t usersbulletType = 1;//生成子弹的类型
//  if(JoyStick.ButtonD==1 )//武器攻击
//  {
//    osMessageQueuePut(UsersbulletQueueHandle, &usersbulletType, 1, 0);
//  }
//  
//}



//玩家射击
void usersShoot(void)
{
  static uint8_t usersbulletType = 0;//生成子弹的类型
  static uint8_t usersEnergyFlag = 0;//角色能量标志
  static uint8_t usersEnergycnt = 0, usersNotShootcnt = 0;//角色能量相关计数
  
  if(weaponType==0){usersbulletType=1;}
  if(weaponType==1){usersbulletType=2;}
  
  if(JoyStick.ButtonD==1 && usersrole->Energy>0)//开枪射击
  {
    osMessageQueuePut(UsersbulletQueueHandle, &usersbulletType, 1, 0);
    usersEnergyFlag = 0;
    usersNotShootcnt = 0;
  }
  if(usersEnergyFlag==1 && usersrole->Energy!=usersrole->MaxEnergy)//开始回复蓝量
  {
    if(usersEnergycnt%2 == 0){usersrole->Energy++;}
    usersEnergycnt++;
  }
  if(usersNotShootcnt<20 && usersEnergyFlag==0){usersNotShootcnt++;}//玩家不射击计数
  if(usersrole->Energy<usersrole->MaxEnergy && usersEnergyFlag==0 && usersNotShootcnt==20){usersEnergyFlag = 1; usersEnergycnt = 0; usersNotShootcnt = 0;}//判断回复蓝量的条件
}

//玩家护盾回复
void usersShieldRecover(void)
{
  static uint8_t usersShieldLast, recordShieldFlag = 1;//之前的盾值
  static uint8_t usersShieldNotMaxcnt = 0,usersShieldRecnt = 1, usersShieldFlag = 0;
  static uint8_t usersHealthLast;
  
  if(usersrole->Shield<usersrole->MaxShield)//角色受伤了
  {
    if(recordShieldFlag){usersShieldLast = usersrole->Shield; usersHealthLast = usersrole->Health; recordShieldFlag = 0;}
    usersShieldNotMaxcnt++;
    if(usersrole->Shield==usersShieldLast && usersrole->Health==usersHealthLast)
    {
      if(usersShieldNotMaxcnt%40==0){usersShieldFlag = 1; usersShieldNotMaxcnt = 0;}//角色没有再受伤
    }
    else if(usersrole->Shield<usersShieldLast && usersrole->Health<=usersHealthLast)//角色再次受伤,重置计数
    {
      usersShieldLast = usersrole->Shield;
      usersHealthLast = usersrole->Health; 
      usersShieldNotMaxcnt = 0; 
      usersShieldFlag = 0;
    }
    else if(usersrole->Shield==0 && usersShieldLast==0)//盾为0要检测扣血
    {
      usersHealthLast = usersrole->Health; 
      usersShieldNotMaxcnt = 0; 
      usersShieldFlag = 0;
    }
  }
  
  if(usersShieldFlag)//回复盾值
  {
    if(usersShieldRecnt%15==0 && usersrole->Shield<usersrole->MaxShield)
    {
      usersrole->Shield++;
      usersShieldLast = usersrole->Shield;
      usersShieldRecnt = 0;
    }
    usersShieldRecnt++;
    if(usersrole->Shield==usersrole->MaxShield){usersShieldFlag = 0; recordShieldFlag = 1;}//盾满了停止回复并且重置记录上一次受伤盾数量的标志
  }
}

//TODO 敌人的移动
void enemies_Move(void)
{
  //最简单的实现方法就是每个时刻敌人都朝着玩家走
  static float DistTemp = 0;  //计算<敌人质心>与<玩家角色质心>的直线距离
  //uint16_t enemies_distance = 50;  //设定敌人间的最小距离, 小于此距离会触发敌人碰撞预防程序

  //enemies_and_mapblock_Crash();  //先做敌人与地图碰撞的检查
  //可以不用在enemies_Move()函数里检查敌人碰撞,即可以先使用enemies_Move()让敌人移动,
  //enemies_Move()函数运行之后再在下一个函数里检查碰撞
	for(uint8_t j=0; j<ENEMIES_MAX-2; j++)  //遍历敌人,Boss和护盾不移动
  {
    //VelTemp = sqrt(enemies[j].Vel_x * enemies[j].Vel_x + enemies[j].Vel_y * enemies[j].Vel_y);
    if(enemies[j].Health)
    {
      //enemies_distance = straight_distance(enemies[i].Center_x, enemies[i].Center_y, enemies[j].Center_x, enemies[j].Center_y);
      DistTemp = straight_distance(enemies[j].Center_x,enemies[j].Center_y,usersrole->Center_x,usersrole->Center_y);
      enemies[j].Vel_x = ((float)(usersrole->Center_x-enemies[j].Center_x)/DistTemp) * 2;
      enemies[j].Vel_y = ((float)(usersrole->Center_y-enemies[j].Center_y)/DistTemp) * 2;
      enemies[j].lastCenter_x = enemies[j].Center_x;
      enemies[j].lastCenter_y = enemies[j].Center_y;
      enemies[j].Center_x += roundNum(enemies[j].Vel_x);
      enemies[j].Center_y += roundNum(enemies[j].Vel_y);
      enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
      enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
    }
    //enemiesbullet_and_mapblock_Crash();
  }
  
  //玩家与地图的碰撞函数也有问题：玩家会突然瞬移,需要改进碰撞检测算法
  //追踪玩家一段时间后,敌人会重叠在一起
  //考虑增大敌人的碰撞半径,额外写一个敌人与敌人相互碰撞的函数
}

void boss_Move(void)
{
  static float bossDistTemp = 0;  
	if(enemies[ENEMIES_MAX-1].Health&&enemies[ENEMIES_MAX-1].Health<5)
    {
      bossDistTemp = straight_distance(enemies[ENEMIES_MAX-1].Center_x,enemies[ENEMIES_MAX-1].Center_y,usersrole->Center_x,usersrole->Center_y);
      enemies[ENEMIES_MAX-1].Vel_x = ((float)(usersrole->Center_x-enemies[ENEMIES_MAX-1].Center_x)/bossDistTemp) * 2;
      enemies[ENEMIES_MAX-1].Vel_y = ((float)(usersrole->Center_y-enemies[ENEMIES_MAX-1].Center_y)/bossDistTemp) * 2;
      enemies[ENEMIES_MAX-1].lastCenter_x = enemies[ENEMIES_MAX-1].Center_x;
      enemies[ENEMIES_MAX-1].lastCenter_y = enemies[ENEMIES_MAX-1].Center_y;
      enemies[ENEMIES_MAX-1].Center_x += roundNum(enemies[ENEMIES_MAX-1].Vel_x);
      enemies[ENEMIES_MAX-1].Center_y += roundNum(enemies[ENEMIES_MAX-1].Vel_y);
      enemies[ENEMIES_MAX-1].Vertec_x = enemies[ENEMIES_MAX-1].Center_x - enemies[ENEMIES_MAX-1].Pixel_x/2;
      enemies[ENEMIES_MAX-1].Vertec_y = enemies[ENEMIES_MAX-1].Center_y - enemies[ENEMIES_MAX-1].Pixel_y/2;
  }
}


/****************碰撞检测****************/
//玩家与地图方块
void users_and_mapblock_Crash(void)
{
  int16_t usersSpot1[2]={-1, -1}, usersSpot2[2]={-1, -1};//玩家位置的修正点 x,y
  static int16_t distanceToUsersLastSpot1, distanceToUsersLastSpot2;
  static float spotTemp;
  
  //玩家不能超越边界
  if(usersrole->Vertec_x <= mapBoundary_Left)  //左边界
  {usersrole->Vertec_x = mapBoundary_Left; usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;}
  
  if(usersrole->Vertec_x+usersrole->Pixel_x >= mapBoundary_Right)  //右边界
  {usersrole->Vertec_x = mapBoundary_Right - usersrole->Pixel_x; 
   usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;}
  
  if(usersrole->Vertec_y <= mapBoundary_Up)  //上边界
  {usersrole->Vertec_y = mapBoundary_Up;
   usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
   usersweapon->wVertec_y = usersrole->Center_y;}
  
  if(usersrole->Vertec_y+usersrole->Pixel_y >= mapBoundary_Down)  //下边界
  {usersrole->Vertec_y = mapBoundary_Down - usersrole->Pixel_y; 
   usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
   usersweapon->wVertec_y = usersrole->Center_y;}

  //玩家与地图障碍的碰撞
  //用角色当前坐标(已经进入到了障碍内部)与上一帧坐标做直线求与物块四边的交点并进行修正
  for(uint8_t i=0; i<MAPBARRIER_MAX; i++)
  {
    if(mapBarrier[i].isCreated == 0){break;}
    if(usersrole->Center_x >= mapBarrier[i].LeftBoundary-usersrole->Pixel_x/2 && usersrole->Center_x <= mapBarrier[i].RightBoundary+usersrole->Pixel_x/2 && //左右边界
       usersrole->Center_y >= mapBarrier[i].UpBoundary-usersrole->Pixel_y/2   && usersrole->Center_y <= mapBarrier[i].DownBoundary+usersrole->Pixel_y/2)    //上下边界
    {
      if(fabs(usersrole->Vel_x)>0.0f && usersrole->Vel_y==0.0f)      //特殊情况1 速度是沿着x轴的
      {
        usersSpot1[0] = mapBarrier[i].LeftBoundary - usersrole->Pixel_x/2 - 1;//多一个像素为了防止重复判断而位置突变
        usersSpot1[1] = usersrole->Center_y;
        usersSpot2[0] = mapBarrier[i].RightBoundary + usersrole->Pixel_x/2 + 1;
        usersSpot2[1] = usersrole->Center_y;
        distanceToUsersLastSpot1 = straight_distance(usersSpot1[0], usersSpot1[1], usersrole->lastCenter_x, usersrole->lastCenter_y);
        distanceToUsersLastSpot2 = straight_distance(usersSpot2[0], usersSpot2[1], usersrole->lastCenter_x, usersrole->lastCenter_y);
        if(distanceToUsersLastSpot1 <= distanceToUsersLastSpot2)
        {
          usersrole->Center_x = usersSpot1[0];
          usersrole->Center_y = usersSpot1[1];
          usersrole->Vertec_x = usersrole->Center_x - usersrole->Pixel_x/2;
          usersrole->Vertec_y = usersrole->Center_y - usersrole->Pixel_y/2;
        }
        else
        {
          usersrole->Center_x = usersSpot2[0];
          usersrole->Center_y = usersSpot2[1];
          usersrole->Vertec_x = usersrole->Center_x - usersrole->Pixel_x/2;
          usersrole->Vertec_y = usersrole->Center_y - usersrole->Pixel_y/2;
        }
        break;//一次只和一个物块碰撞
      }
      else if(fabs(usersrole->Vel_y)>0.0f && usersrole->Vel_x==0.0f)   //特殊情况2 速度是沿着y轴的
      {
        usersSpot1[0] = usersrole->Center_x;
        usersSpot1[1] = mapBarrier[i].UpBoundary - usersrole->Pixel_y/2 - 1;//多一个像素为了防止重复判断而位置突变
        usersSpot2[0] = usersrole->Center_x;
        usersSpot2[1] = mapBarrier[i].DownBoundary + usersrole->Pixel_y/2 + 1;
        distanceToUsersLastSpot1 = straight_distance(usersSpot1[0], usersSpot1[1], usersrole->lastCenter_x, usersrole->lastCenter_y);
        distanceToUsersLastSpot2 = straight_distance(usersSpot2[0], usersSpot2[1], usersrole->lastCenter_x, usersrole->lastCenter_y);
        if(distanceToUsersLastSpot1 <= distanceToUsersLastSpot2)
        {
          usersrole->Center_x = usersSpot1[0];
          usersrole->Center_y = usersSpot1[1];
          usersrole->Vertec_x = usersrole->Center_x - usersrole->Pixel_x/2;
          usersrole->Vertec_y = usersrole->Center_y - usersrole->Pixel_y/2;
        }
        else
        {
          usersrole->Center_x = usersSpot2[0];
          usersrole->Center_y = usersSpot2[1];
          usersrole->Vertec_x = usersrole->Center_x - usersrole->Pixel_x/2;
          usersrole->Vertec_y = usersrole->Center_y - usersrole->Pixel_y/2;
        }
        break;//一次只和一个物块碰撞
      }
      else if(fabs(usersrole->Vel_x)>0.0f && fabs(usersrole->Vel_y)>0.0f)
      {
        //计算与物块上边界的交点 x
        spotTemp = (float)(mapBarrier[i].UpBoundary-usersrole->Pixel_y/2-usersrole->Center_y)*(usersrole->lastCenter_x-usersrole->Center_x)/(usersrole->lastCenter_y-usersrole->Center_y)+usersrole->Center_x;
        if(spotTemp >= mapBarrier[i].LeftBoundary-usersrole->Pixel_x/2 && spotTemp <= mapBarrier[i].RightBoundary+usersrole->Pixel_x/2)
        {
          usersSpot1[0] = roundNum(spotTemp);//四舍五入
          usersSpot1[1] = mapBarrier[i].UpBoundary - usersrole->Pixel_y/2 - 1;//多一个像素为了防止重复判断而位置突变
        }
        //计算与物块下边界的交点 x
        spotTemp = (float)(mapBarrier[i].DownBoundary+usersrole->Pixel_y/2-usersrole->Center_y)*(usersrole->lastCenter_x-usersrole->Center_x)/(usersrole->lastCenter_y-usersrole->Center_y)+usersrole->Center_x;
        if(spotTemp >= mapBarrier[i].LeftBoundary-usersrole->Pixel_x/2 && spotTemp <= mapBarrier[i].RightBoundary+usersrole->Pixel_x/2)
        {
          if(usersSpot1[0] == -1 && usersSpot1[1] == -1)
          {
            usersSpot1[0] = roundNum(spotTemp);//四舍五入
            usersSpot1[1] = mapBarrier[i].DownBoundary + usersrole->Pixel_y/2 + 1;//多一个像素为了防止重复判断而位置突变
          }
          else if(usersSpot2[0] == -1 && usersSpot2[1] == -1)
          {
            usersSpot2[0] = roundNum(spotTemp);//四舍五入
            usersSpot2[1] = mapBarrier[i].DownBoundary + usersrole->Pixel_y/2 + 1;
          }
        }
        //计算与物块左边界的交点 y
        spotTemp = (float)(mapBarrier[i].LeftBoundary-usersrole->Pixel_x/2-usersrole->Center_x)*(usersrole->lastCenter_y-usersrole->Center_y)/(usersrole->lastCenter_x-usersrole->Center_x)+usersrole->Center_y;
        if(spotTemp >= mapBarrier[i].UpBoundary-usersrole->Pixel_y/2 && spotTemp <= mapBarrier[i].DownBoundary+usersrole->Pixel_y/2)
        {
          if(usersSpot1[0] == -1 && usersSpot1[1] == -1)
          {
            usersSpot1[0] = mapBarrier[i].LeftBoundary - usersrole->Pixel_x/2 - 1;//多一个像素为了防止重复判断而位置突变
            usersSpot1[1] = roundNum(spotTemp);//四舍五入
          }
          else if(usersSpot2[0] == -1 && usersSpot2[1] == -1)
          {
            usersSpot2[0] = mapBarrier[i].LeftBoundary - usersrole->Pixel_x/2 - 1;
            usersSpot2[1] = roundNum(spotTemp);//四舍五入
          }
        }
        //计算与物块右边界的交点 y
        spotTemp = (float)(mapBarrier[i].RightBoundary+usersrole->Pixel_x/2-usersrole->Center_x)*(usersrole->lastCenter_y-usersrole->Center_y)/(usersrole->lastCenter_x-usersrole->Center_x)+usersrole->Center_y;
        if(spotTemp >= mapBarrier[i].UpBoundary-usersrole->Pixel_y/2 && spotTemp <= mapBarrier[i].DownBoundary+usersrole->Pixel_y/2)
        {
          if(usersSpot1[0] == -1 && usersSpot1[1] == -1)
          {
            usersSpot1[0] = mapBarrier[i].RightBoundary + usersrole->Pixel_x/2 + 1;//多一个像素为了防止重复判断而位置突变
            usersSpot1[1] = roundNum(spotTemp);//四舍五入
          }
          else if(usersSpot2[0] == -1 && usersSpot2[1] == -1)
          {
            usersSpot2[0] = mapBarrier[i].RightBoundary + usersrole->Pixel_x/2 + 1;
            usersSpot2[1] = roundNum(spotTemp);//四舍五入
          }
        }
        //计算距离上一帧角色位置最近的点作为修正点
        distanceToUsersLastSpot1 = straight_distance(usersSpot1[0], usersSpot1[1], usersrole->lastCenter_x, usersrole->lastCenter_y);
        distanceToUsersLastSpot2 = straight_distance(usersSpot2[0], usersSpot2[1], usersrole->lastCenter_x, usersrole->lastCenter_y);
        if(distanceToUsersLastSpot1 <= distanceToUsersLastSpot2)
        {
          usersrole->Center_x = usersSpot1[0];
          usersrole->Center_y = usersSpot1[1];
          usersrole->Vertec_x = usersrole->Center_x - usersrole->Pixel_x/2;
          usersrole->Vertec_y = usersrole->Center_y - usersrole->Pixel_y/2;
        }
        else
        {
          usersrole->Center_x = usersSpot2[0];
          usersrole->Center_y = usersSpot2[1];
          usersrole->Vertec_x = usersrole->Center_x - usersrole->Pixel_x/2;
          usersrole->Vertec_y = usersrole->Center_y - usersrole->Pixel_y/2;
        }
        break;//一次只和一个物块碰撞
      }
    }
  }
   
  //TODO 与可破坏地图障碍的碰撞
  
  //TODO 与地刺陷阱的碰撞
}

//TODO 敌人与地图单位的碰撞
void enemies_and_mapblock_Crash(void)
{
  int16_t enemiesSpot1[2]={-1, -1}, enemiesSpot2[2]={-1, -1};//敌人位置的修正点 x,y
  static int16_t distanceToEnemiesLastSpot1, distanceToEnemiesLastSpot2;
  static float spotTemp;
  
  for (uint8_t j=0; j<ENEMIES_MAX; j++)  //<j>为敌人编号的计数
  {
    if (enemies[j].Health)  //先判断敌人是否存在,提高程序运行效率
    {
      //敌人不能超越边界
      if(enemies[j].Vertec_x <= mapBoundary_Left)  //左边界
        {enemies[j].Vertec_x = mapBoundary_Left; enemies[j].Center_x = enemies[j].Vertec_x + enemies[j].Pixel_x/2;}
    
      if(enemies[j].Vertec_x+enemies[j].Pixel_x >= mapBoundary_Right)  //右边界
        {enemies[j].Vertec_x = mapBoundary_Right - enemies[j].Pixel_x; 
         enemies[j].Center_x = enemies[j].Vertec_x + enemies[j].Pixel_x/2;}
    
      if(enemies[j].Vertec_y <= mapBoundary_Up)  //上边界
        {enemies[j].Vertec_y = mapBoundary_Up;
         enemies[j].Center_y = enemies[j].Vertec_y + enemies[j].Pixel_y/2;}
         //？？enemy有没有武器？这行要怎么改？usersweapon->wVertec_y = usersrole->Center_y;}
    
      if(enemies[j].Vertec_y+enemies[j].Pixel_y >= mapBoundary_Down)  //下边界
        {enemies[j].Vertec_y = mapBoundary_Down - enemies[j].Pixel_y; 
         enemies[j].Center_y = enemies[j].Vertec_y + enemies[j].Pixel_y/2;}
         //？？enemy有没有武器？这行要怎么改？usersweapon->wVertec_y = usersrole->Center_y;}

         //敌人与地图障碍的碰撞
         //用敌人当前坐标(已经进入到了障碍内部)与上一帧坐标做直线求与物块四边的交点并进行修正
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++)
      {
        if(mapBarrier[i].isCreated == 0){break;}
        if(enemies[j].Center_x >= mapBarrier[i].LeftBoundary-enemies[j].Pixel_x/2 && enemies[j].Center_x <= mapBarrier[i].RightBoundary+enemies[j].Pixel_x/2 && //左右边界
           enemies[j].Center_y >= mapBarrier[i].UpBoundary-enemies[j].Pixel_y/2   && enemies[j].Center_y <= mapBarrier[i].DownBoundary+enemies[j].Pixel_y/2)    //上下边界
          {
            if(fabs(enemies[j].Vel_x)>0.0f && enemies[j].Vel_y==0.0f)      //特殊情况1 速度是沿着x轴的
            {
              enemiesSpot1[0] = mapBarrier[i].LeftBoundary - enemies[j].Pixel_x/2 - 1;//多一个像素为了防止重复判断而位置突变
              enemiesSpot1[1] = enemies[j].Center_y;
              enemiesSpot2[0] = mapBarrier[i].RightBoundary + enemies[j].Pixel_x/2 + 1;
              enemiesSpot2[1] = enemies[j].Center_y;
              distanceToEnemiesLastSpot1 = straight_distance(enemiesSpot1[0], enemiesSpot1[1], enemies[j].lastCenter_x, enemies[j].lastCenter_y);
              distanceToEnemiesLastSpot2 = straight_distance(enemiesSpot2[0], enemiesSpot2[1], enemies[j].lastCenter_x, enemies[j].lastCenter_y);
              if(distanceToEnemiesLastSpot1 <= distanceToEnemiesLastSpot2)
              {
                enemies[j].Center_x = enemiesSpot1[0];
                enemies[j].Center_y = enemiesSpot1[1];
                enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
                enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
              }
              else
              {
                enemies[j].Center_x = enemiesSpot2[0];
                enemies[j].Center_y = enemiesSpot2[1];
                enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
                enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
              }
              break;//一次只和一个物块碰撞
            }
            else if(fabs(enemies[j].Vel_y)>0.0f && enemies[j].Vel_x==0.0f)   //特殊情况2 速度是沿着y轴的
            {
              enemiesSpot1[0] = enemies[j].Center_x;
              enemiesSpot1[1] = mapBarrier[i].UpBoundary - enemies[j].Pixel_y/2 - 1;//多一个像素为了防止重复判断而位置突变
              enemiesSpot2[0] = enemies[j].Center_x;
              enemiesSpot2[1] = mapBarrier[i].DownBoundary + enemies[j].Pixel_y/2 + 1;
              distanceToEnemiesLastSpot1 = straight_distance(enemiesSpot1[0], enemiesSpot1[1], enemies[j].lastCenter_x, enemies[j].lastCenter_y);
              distanceToEnemiesLastSpot2 = straight_distance(enemiesSpot2[0], enemiesSpot2[1], enemies[j].lastCenter_x, enemies[j].lastCenter_y);
              if(distanceToEnemiesLastSpot1 <= distanceToEnemiesLastSpot2)
              {
                enemies[j].Center_x = enemiesSpot1[0];
                enemies[j].Center_y = enemiesSpot1[1];
                enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
                enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
              }
              else
              {
                enemies[j].Center_x = enemiesSpot2[0];
                enemies[j].Center_y = enemiesSpot2[1];
                enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
                enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
              }
              break;//一次只和一个物块碰撞
            }
            else if(fabs(enemies[j].Vel_x)>0.0f && fabs(enemies[j].Vel_y)>0.0f)
            {
              //计算与物块上边界的交点 x
              spotTemp = (float)(mapBarrier[i].UpBoundary-enemies[j].Pixel_y/2-enemies[j].Center_y)*(enemies[j].lastCenter_x-enemies[j].Center_x)/(enemies[j].lastCenter_y-enemies[j].Center_y)+enemies[j].Center_x;
              if(spotTemp >= mapBarrier[i].LeftBoundary-enemies[j].Pixel_x/2 && spotTemp <= mapBarrier[i].RightBoundary+enemies[j].Pixel_x/2)
              {
                enemiesSpot1[0] = roundNum(spotTemp);//四舍五入
                enemiesSpot1[1] = mapBarrier[i].UpBoundary - enemies[j].Pixel_y/2 - 1;//多一个像素为了防止重复判断而位置突变
              }
              //计算与物块下边界的交点 x
              spotTemp = (float)(mapBarrier[i].DownBoundary+enemies[j].Pixel_y/2-enemies[j].Center_y)*(enemies[j].lastCenter_x-enemies[j].Center_x)/(enemies[j].lastCenter_y-enemies[j].Center_y)+enemies[j].Center_x;
              if(spotTemp >= mapBarrier[i].LeftBoundary-enemies[j].Pixel_x/2 && spotTemp <= mapBarrier[i].RightBoundary+enemies[j].Pixel_x/2)
              {
                if(enemiesSpot1[0] == -1 && enemiesSpot1[1] == -1)
                {
                  enemiesSpot1[0] = roundNum(spotTemp);//四舍五入
                  enemiesSpot1[1] = mapBarrier[i].DownBoundary + enemies[j].Pixel_y/2 + 1;//多一个像素为了防止重复判断而位置突变
                }
                else if(enemiesSpot2[0] == -1 && enemiesSpot2[1] == -1)
                {
                  enemiesSpot2[0] = roundNum(spotTemp);//四舍五入
                  enemiesSpot2[1] = mapBarrier[i].DownBoundary + enemies[j].Pixel_y/2 + 1;
                }
              }
              //计算与物块左边界的交点 y
              spotTemp = (float)(mapBarrier[i].LeftBoundary-enemies[j].Pixel_x/2-enemies[j].Center_x)*(enemies[j].lastCenter_y-enemies[j].Center_y)/(enemies[j].lastCenter_x-enemies[j].Center_x)+enemies[j].Center_y;
              if(spotTemp >= mapBarrier[i].UpBoundary-enemies[j].Pixel_y/2 && spotTemp <= mapBarrier[i].DownBoundary+enemies[j].Pixel_y/2)
              {
                if(enemiesSpot1[0] == -1 && enemiesSpot1[1] == -1)
                {
                  enemiesSpot1[0] = mapBarrier[i].LeftBoundary - enemies[j].Pixel_x/2 - 1;//多一个像素为了防止重复判断而位置突变
                  enemiesSpot1[1] = roundNum(spotTemp);//四舍五入
                }
                else if(enemiesSpot2[0] == -1 && enemiesSpot2[1] == -1)
                {
                  enemiesSpot2[0] = mapBarrier[i].LeftBoundary - enemies[j].Pixel_x/2 - 1;
                  enemiesSpot2[1] = roundNum(spotTemp);//四舍五入
                }
              }
              //计算与物块右边界的交点 y
              spotTemp = (float)(mapBarrier[i].RightBoundary+enemies[j].Pixel_x/2-enemies[j].Center_x)*(enemies[j].lastCenter_y-enemies[j].Center_y)/(enemies[j].lastCenter_x-enemies[j].Center_x)+enemies[j].Center_y;
              if(spotTemp >= mapBarrier[i].UpBoundary-enemies[j].Pixel_y/2 && spotTemp <= mapBarrier[i].DownBoundary+enemies[j].Pixel_y/2)
              {
                if(enemiesSpot1[0] == -1 && enemiesSpot1[1] == -1)
                {
                  enemiesSpot1[0] = mapBarrier[i].RightBoundary + enemies[j].Pixel_x/2 + 1;//多一个像素为了防止重复判断而位置突变
                  enemiesSpot1[1] = roundNum(spotTemp);//四舍五入
                }
                else if(enemiesSpot2[0] == -1 && enemiesSpot2[1] == -1)
                {
                  enemiesSpot2[0] = mapBarrier[i].RightBoundary + enemies[j].Pixel_x/2 + 1;
                  enemiesSpot2[1] = roundNum(spotTemp);//四舍五入
                }
              }
              //计算距离上一帧角色位置最近的点作为修正点
              distanceToEnemiesLastSpot1 = straight_distance(enemiesSpot1[0], enemiesSpot1[1], enemies[j].lastCenter_x, enemies[j].lastCenter_y);
              distanceToEnemiesLastSpot2 = straight_distance(enemiesSpot2[0], enemiesSpot2[1], enemies[j].lastCenter_x, enemies[j].lastCenter_y);
              if(distanceToEnemiesLastSpot1 <= distanceToEnemiesLastSpot2)
              {
                enemies[j].Center_x = enemiesSpot1[0];
                enemies[j].Center_y = enemiesSpot1[1];
                enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
                enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
              }
              else
              {
                enemies[j].Center_x = enemiesSpot2[0];
                enemies[j].Center_y = enemiesSpot2[1];
                enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
                enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
              }
              break;//一次只和一个物块碰撞
            }
        }
      }  //end for：结束对地图障碍的遍历
    }  //end if：结束对敌人是否存在的判断
  }  //end for：结束对敌人编号的遍历
  
  //TODO 与可破坏地图障碍的碰撞
  
  //TODO 与地刺陷阱的碰撞
}

//TODO 敌人与敌人的碰撞
void enemies_and_enemies_Crash(void) //MXS: 这里如果两个敌人碰在一起就不能动了，还有问题！
{
  //随着敌人对玩家的追踪,敌人有重叠在一起的趋势 (各个敌人的质心坐标趋于一致)
  //所以,需要为敌人加入碰撞半径,使敌人靠近到一定程度就无法靠近
  
  uint16_t enemies_distance = 50;  //设定敌人间的最小距离, 小于此距离会触发敌人碰撞预防程序
  int16_t midPoint_x = 0;  //两敌人质心连线中点的x坐标
  int16_t midPoint_y = 0;  //两敌人质心连线中点的y坐标
  //static float DistTemp = 0;  //计算<敌人质心连线中点>与<玩家角色质心>的直线距离
  float Vel_prop = 0.05;  //定义一比例系数，用于计算敌人移动终点的坐标

  
  for(uint8_t i=0; i<ENEMIES_MAX-1; i++)  //两两一组,遍历敌人
  {
    if (enemies[i].Health&&i!=ENEMIES_MAX-2)  //如果敌人存在,且不为boss护盾
    {
      for(uint8_t j=i+1; j<ENEMIES_MAX; j++)
      {
        if ((straight_distance(enemies[i].Center_x, enemies[i].Center_y, enemies[j].Center_x, enemies[j].Center_y)) < enemies_distance)
        {
          enemies[i].Center_x = enemies[i].lastCenter_x;
          enemies[i].Center_y = enemies[i].lastCenter_y;  //enemies[i]回到上一帧的位置
          enemies[i].Vertec_x = enemies[i].Center_x - enemies[i].Pixel_x/2;
          enemies[i].Vertec_y = enemies[i].Center_y - enemies[i].Pixel_y/2;
          enemies[j].Center_x = enemies[j].lastCenter_x;
          enemies[j].Center_y = enemies[j].lastCenter_y;  //enemies[j]回到上一帧的位置
          enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
          enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
          
          //敌人回到上一帧的位置后, 沿质心中点与玩家角色连线方向移动一段距离
          midPoint_x = (enemies[i].Center_x + enemies[j].Center_x)/2;
          midPoint_y = (enemies[i].Center_y + enemies[j].Center_y)/2;
          enemies[i].Center_x = enemies[i].Center_x + roundNum(Vel_prop*(float)(usersrole->Center_x-midPoint_x));
          enemies[i].Center_y = enemies[i].Center_y + roundNum(Vel_prop*(float)(usersrole->Center_y-midPoint_y));
          enemies[j].Center_x = enemies[j].Center_x + roundNum(Vel_prop*(float)(usersrole->Center_x-midPoint_x));
          enemies[j].Center_y = enemies[j].Center_y + roundNum(Vel_prop*(float)(usersrole->Center_y-midPoint_y));
          enemies[i].Vertec_x = enemies[i].Center_x - enemies[i].Pixel_x/2;
          enemies[i].Vertec_y = enemies[i].Center_y - enemies[i].Pixel_y/2;
          enemies[j].Vertec_x = enemies[j].Center_x - enemies[j].Pixel_x/2;
          enemies[j].Vertec_y = enemies[j].Center_y - enemies[j].Pixel_y/2;
        }
      }
    }
  }
}

//玩家与敌方单位
void users_and_enemies_Crash(void)
{
  uint16_t users_and_enemies_distance;
  float Vel_prop = 1.0;  //定义一比例系数，用于计算玩家角色后退之后的移动终点的坐标
  static uint8_t timeCount = 0;  //硬直时间计时
  static uint8_t timeLimit = 7;  //硬直时间限度
  static uint16_t invincibleFrame = 15;  //硬直导致的无敌帧时间计时(这个无敌仅仅是指玩家可以移动,如果与敌人碰撞还是会受到伤害)
  static uint16_t invincibleFrameLimit = 15;  //硬直导致的无敌帧时间限度(30太小了,玩家逃不掉)
  static int16_t tempCenter_x = 0;  //保存硬直期间角色的x坐标
  static int16_t tempCenter_y = 0;  //保存硬直期间角色的y坐标
  static int16_t damageCount = 0;  //硬直结束但无敌帧未结束期间玩家受击频率控制
  static int16_t damageLimit = 30;  //硬直结束但无敌帧未结束期间玩家受击频率限度
  
  for(uint8_t i=0; i<ENEMIES_MAX; i++)
  {
    if((usersrole->Health>0)&(enemies[i].Health>0))  //如果玩家角色和敌人都存在
    {
      users_and_enemies_distance = straight_distance(usersrole->Center_x, usersrole->Center_y, enemies[i].Center_x, enemies[i].Center_y);
      if((users_and_enemies_distance <= (usersrole->Crash_r+enemies[i].Crash_r))&(timeCount==0)&(invincibleFrame==invincibleFrameLimit))
      {//碰撞且玩家不处于硬直状态且无敌帧已经结束
        invincibleFrame = 0;
        if(usersrole->Shield)
        {
          usersrole->Shield--;
        }
        else
        {
          usersrole->Health--;
        }
        //TODO:硬直并退后(玩家角色)
        usersrole->Center_x = usersrole->Center_x + roundNum(Vel_prop*((float)(usersrole->Center_x - enemies[i].Center_x)));
        usersrole->Center_y = usersrole->Center_y + roundNum(Vel_prop*((float)(usersrole->Center_y - enemies[i].Center_y)));
        usersrole->Vertec_x = usersrole->Center_x - (usersrole->Pixel_x)/2;
        usersrole->Vertec_y = usersrole->Center_y - (usersrole->Pixel_y)/2;
        tempCenter_x = usersrole->Center_x;
        tempCenter_y = usersrole->Center_y;
        timeCount += 1;
        //break;
      }
      else if((users_and_enemies_distance <= (usersrole->Crash_r+enemies[i].Crash_r))&(timeCount!=0)&(invincibleFrame!=invincibleFrameLimit))
      {//碰撞且玩家不处于硬直状态且无敌帧还未结束,此时玩家会受到伤害(但是玩家可以移动躲避敌人)
        if(damageCount==damageLimit)
        {
          damageCount = 0;
          if(usersrole->Shield)
          {
            usersrole->Shield--;
          }
          else
          {
            usersrole->Health--;
          }
        }
        else
        {
          damageCount += 1;
        }
      }
    }
  }  //for循环遍历敌人结束
  if(timeCount!=0)//硬直期间
  {
    //在与敌人脱离后的一段时间内,玩家角色无法移动
    if(timeCount==timeLimit)  //硬直时间结束
    {
      timeCount = 0;
    }
    else
    {
      usersrole->Center_x = tempCenter_x;  //玩家硬直停留的位置就是玩家第一个碰撞的敌人的质心坐标
      usersrole->Center_y = tempCenter_y;
      usersrole->Vertec_x = usersrole->Center_x - (usersrole->Pixel_x)/2;
      usersrole->Vertec_y = usersrole->Center_y - (usersrole->Pixel_y)/2;
      timeCount += 1;
    }
  }
  invincibleFrame += 1;  //无敌帧时间计时器+1
  if(invincibleFrame>invincibleFrameLimit)
  {
    invincibleFrame = invincibleFrameLimit;
  }
}

//玩家与敌方子弹
void users_and_enemiesbullet_Crash(void)
{
  static uint16_t enemiesbul_crash_distance;

  for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++)
  {
    if((usersrole->Health>0) && (enemiesbullet[i].isExist==1))
    {
      enemiesbul_crash_distance = straight_distance(usersrole->Center_x, usersrole->Center_y, enemiesbullet[i].bCenter_x, enemiesbullet[i].bCenter_y);
      if(enemiesbul_crash_distance <= (usersrole->Crash_r+enemiesbullet[i].bCrash_r))
      {
        enemiesbullet[i].bVertec_x = 330;//移除子弹
        enemiesbullet[i].bVertec_y = 250;
        enemiesbullet[i].isExist = 0;
        if(usersrole->Shield){usersrole->Shield -= enemiesbullet[i].bDamage;if(usersrole->Shield<0){usersrole->Shield=0;}}
        else{usersrole->Health -= enemiesbullet[i].bDamage;if(usersrole->Health<0){usersrole->Health=0;}}
      }
    }
  }
}

//玩家子弹与敌方单位
void usersbullet_and_enemies_Crash(void)
{
  uint16_t usersbul_crash_distance;
  
  for(uint8_t i=0; i<ENEMIES_MAX; i++)//遍历敌人
  {
    if(enemies[i].Health)
    {
      for(uint8_t j=0; j<USERSBULLET_MAX; j++)//遍历玩家子弹
      {
        if(usersbullet[j].isExist)
        {
          usersbul_crash_distance = straight_distance(usersbullet[j].bCenter_x, usersbullet[j].bCenter_y, enemies[i].Center_x, enemies[i].Center_y);
          if(usersbul_crash_distance <= (usersbullet[j].bCrash_r+enemies[i].Crash_r))
          {
            usersbullet[j].bVertec_x = 330;
            usersbullet[j].bVertec_y = 250;
            usersbullet[j].isExist = 0;
            if(enemies[i].Health)
            {
              enemies[i].Health -= usersbullet[j].bDamage;
              if(enemies[i].Health < 0){enemies[i].Health = 0;}
            }
          }
        }
      }
    }
  }
}

void usersattack_and_enemies_Crash(void)
{
  uint16_t usersattack_crash_distance;
  uint16_t usersattack2_crash_distance;
  uint16_t users_crash_distance;
  
  for(uint8_t i=0; i<ENEMIES_MAX; i++)//遍历敌人
  {
    if(enemies[i].Health)
    {
        if(usersattack.isExist)
        {
          usersattack_crash_distance = straight_distance(usersattack.bCenter_x, usersattack.bCenter_y, enemies[i].Center_x, enemies[i].Center_y);
          if(usersattack_crash_distance <= (usersattack.bCrash_r+enemies[i].Crash_r))
          {
            usersattack.bVertec_x = 330;
            usersattack.bVertec_y = 250;
            usersattack.isExist = 0;
            if(enemies[i].Health)
            {
              enemies[i].Health -= usersattack.bDamage;
              if(enemies[i].Health < 0){enemies[i].Health = 0;}
            }
          }
        }
    }
  }
    if((usersrole->Health>0) && (usersattack.isExist==1))
    {
      users_crash_distance = straight_distance(usersrole->Center_x, usersrole->Center_y, usersattack.bCenter_x, usersattack.bCenter_y);
      if(users_crash_distance <= (usersrole->Crash_r+usersattack.bCrash_r))
      {
            usersattack.bVertec_x = 330;
            usersattack.bVertec_y = 250;
            usersattack.isExist = 0;
      }
    }
    
    for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++)
  {
    if((usersrole->Health>0) && (enemiesbullet[i].isExist==1))
    {
      usersattack2_crash_distance = straight_distance(usersattack.bCenter_x, usersattack.bCenter_y, enemiesbullet[i].bCenter_x, enemiesbullet[i].bCenter_y);
      if(usersattack2_crash_distance <= (usersattack.bCrash_r+enemiesbullet[i].bCrash_r))
      {
        enemiesbullet[i].bVertec_x = 330;//移除子弹
        enemiesbullet[i].bVertec_y = 250;
        enemiesbullet[i].isExist = 0;
      }
    }
  }
}



//玩家子弹与墙壁的碰撞
void usersbullet_and_mapblock_Crash(void)
{
  for(uint8_t i=0; i<USERSBULLET_MAX; i++)//与地图边界的碰撞
  {
    if(usersbullet[i].isExist)
    {
      if(usersbullet[i].bCenter_x >= mapBoundary_Right - usersbullet[i].bPixel_x/2 || //右边界
         usersbullet[i].bCenter_x <= mapBoundary_Left + usersbullet[i].bPixel_x/2  || //左边界
         usersbullet[i].bCenter_y >= mapBoundary_Down - usersbullet[i].bPixel_y/2  || //下边界
         usersbullet[i].bCenter_y <= mapBoundary_Up + usersbullet[i].bPixel_y/2)      //上边界
      {
        usersbullet[i].bVertec_x = 330;//移除子弹
        usersbullet[i].bVertec_y = 250;
        usersbullet[i].isExist = 0;
      }
    }
  }
  for(uint8_t i=0; i<MAPBARRIER_MAX; i++)//与不可破坏地图障碍的碰撞
  {
    if(mapBarrier[i].isCreated == 0){break;}
    for(uint8_t j=0; j<USERSBULLET_MAX; j++)
    {
      if(usersbullet[j].isExist)
      {
        //膨胀障碍物边界
        if(usersbullet[j].bCenter_x <= mapBarrier[i].RightBoundary + usersbullet[j].bPixel_x/2 && //右边界
           usersbullet[j].bCenter_x >= mapBarrier[i].LeftBoundary  - usersbullet[j].bPixel_x/2 && //左边界
           usersbullet[j].bCenter_y <= mapBarrier[i].DownBoundary  + usersbullet[j].bPixel_y/2 && //下边界
           usersbullet[j].bCenter_y >= mapBarrier[i].UpBoundary    - usersbullet[j].bPixel_y/2)   //上边界
        {
          usersbullet[j].bVertec_x = 330;//移除子弹
          usersbullet[j].bVertec_y = 250;
          usersbullet[j].isExist = 0;
        }
      }
    }
  }
  //TODO 与可破坏地图障碍的碰撞
	for(uint8_t i=0; i<MAPDESBARRIER_MAX; i++)//与可破坏地图障碍的碰撞
  {
    if(mapDesBarrier[i].isCreated == 0){break;}
		else if(mapDesBarrier[i].isExist)			
		{
			for(uint8_t j=0; j<USERSBULLET_MAX; j++)
			{
				if(usersbullet[j].isExist)
				{
					//膨胀障碍物边界
					if(usersbullet[j].bCenter_x <= mapDesBarrier[i].RightBoundary + usersbullet[j].bPixel_x/2 && //右边界
						 usersbullet[j].bCenter_x >= mapDesBarrier[i].LeftBoundary  - usersbullet[j].bPixel_x/2 && //左边界
						 usersbullet[j].bCenter_y <= mapDesBarrier[i].DownBoundary  + usersbullet[j].bPixel_y/2 && //下边界
						 usersbullet[j].bCenter_y >= mapDesBarrier[i].UpBoundary    - usersbullet[j].bPixel_y/2)   //上边界
					{
						usersbullet[j].bVertec_x = 330;//移除子弹
						usersbullet[j].bVertec_y = 250;
						usersbullet[j].isExist = 0;
						
						mapDesBarrier[i].Vertec_x = 350;//移除障碍
						mapDesBarrier[i].Vertec_y = 260;
						mapDesBarrier[i].isExist = 0;
					}
				}
			}
		}   
  }
}

//敌人子弹与墙壁的碰撞
void enemiesbullet_and_mapblock_Crash(void)
{
  for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++)//与地图边界的碰撞
  {
    if(enemiesbullet[i].isExist)
    {
      if(enemiesbullet[i].bCenter_x >= mapBoundary_Right - enemiesbullet[i].bPixel_x/2 || //右边界
         enemiesbullet[i].bCenter_x <= mapBoundary_Left + enemiesbullet[i].bPixel_x/2  || //左边界
         enemiesbullet[i].bCenter_y >= mapBoundary_Down - enemiesbullet[i].bPixel_y/2  || //下边界
         enemiesbullet[i].bCenter_y <= mapBoundary_Up + enemiesbullet[i].bPixel_y/2)      //上边界
      {
        enemiesbullet[i].bVertec_x = 330;
        enemiesbullet[i].bVertec_y = 250;
        enemiesbullet[i].isExist = 0;
      }
    }
  }
  for(uint8_t i=0; i<MAPBARRIER_MAX; i++)//与不可破坏地图障碍的碰撞
  {
    if(mapBarrier[i].isCreated == 0){break;}
    for(uint8_t j=0; j<ENEMIESBULLET_MAX; j++)
    {
      if(enemiesbullet[j].isExist)
      {
        //膨胀障碍物边界
        if(enemiesbullet[j].bCenter_x <= mapBarrier[i].RightBoundary + enemiesbullet[j].bPixel_x/2 && //右边界
           enemiesbullet[j].bCenter_x >= mapBarrier[i].LeftBoundary  - enemiesbullet[j].bPixel_x/2 && //左边界
           enemiesbullet[j].bCenter_y <= mapBarrier[i].DownBoundary  + enemiesbullet[j].bPixel_y/2 && //下边界
           enemiesbullet[j].bCenter_y >= mapBarrier[i].UpBoundary    - enemiesbullet[j].bPixel_y/2)   //上边界
        {
          enemiesbullet[j].bVertec_x = 330;
          enemiesbullet[j].bVertec_y = 250;
          enemiesbullet[j].isExist = 0;
        }
      }
    }
  }
  //TODO 与可破坏地图障碍的碰撞
	for(uint8_t i=0; i<MAPDESBARRIER_MAX; i++)//与可破坏地图障碍的碰撞
  {
    if(mapDesBarrier[i].isCreated == 0){break;}
		else if(mapDesBarrier[i].isExist)
		{
			for(uint8_t j=0; j<ENEMIESBULLET_MAX; j++)
			{
				if(enemiesbullet[j].isExist)
				{
					//膨胀障碍物边界
					if(enemiesbullet[j].bCenter_x <= mapDesBarrier[i].RightBoundary + enemiesbullet[j].bPixel_x/2 && //右边界
						 enemiesbullet[j].bCenter_x >= mapDesBarrier[i].LeftBoundary  - enemiesbullet[j].bPixel_x/2 && //左边界
						 enemiesbullet[j].bCenter_y <= mapDesBarrier[i].DownBoundary  + enemiesbullet[j].bPixel_y/2 && //下边界
						 enemiesbullet[j].bCenter_y >= mapDesBarrier[i].UpBoundary    - enemiesbullet[j].bPixel_y/2)   //上边界
					{
						enemiesbullet[j].bVertec_x = 330;//移除子弹
						enemiesbullet[j].bVertec_y = 250;
						enemiesbullet[j].isExist = 0;
						
						mapDesBarrier[i].Vertec_x = 350;//移除障碍
						mapDesBarrier[i].Vertec_y = 260;
						mapDesBarrier[i].isExist = 0;
					}
				}
			}
		}
  }
}

/****************更新画面****************/
//更新地图和玩家状态
void map_and_userscondition_Update(void)
{  
  //加入玩家UI
  statusBarUpdate();
  
  //更新地图物块
  mapBoundaryUpdate();
  mapFloorUpdate();
  mapBarrierUpdate();
	mapDesBarrierUpdate();
}

//更新玩家信息
void users_Update(void)
{
  LCD_DrawPic_Transparent(usersrole->Vertec_x, usersrole->Vertec_y, usersrole->Pixel_x, usersrole->Pixel_y, usersrole->pPic);//画人物
  if(usersrole->Health){LCD_DrawPic_Transparent(usersweapon->wVertec_x, usersweapon->wVertec_y, usersweapon->wPixel_x, usersweapon->wPixel_y, usersweapon->pPic);}//画武器
}

//更新敌人信息
void enemies_Update(void)
{
  for(uint8_t i=0; i<ENEMIES_MAX; i++)
  {
    if(enemies[i].Health){LCD_DrawPic_Transparent(enemies[i].Vertec_x, enemies[i].Vertec_y, enemies[i].Pixel_x, enemies[i].Pixel_y, enemies[i].pPic);}
  }
}

//更新子弹信息
void bullet_Update(void)
{
  //玩家子弹
  for(uint8_t i=0; i<USERSBULLET_MAX; i++)
  {
    if(usersbullet[i].isExist)
    {
      LCD_DrawPic_Transparent(usersbullet[i].bVertec_x, usersbullet[i].bVertec_y, usersbullet[i].bPixel_x, usersbullet[i].bPixel_y, usersbullet[i].pPic);
      usersbullet[i].bCenter_x += usersbullet[i].bVel_x;
      usersbullet[i].bVertec_x += usersbullet[i].bVel_x;
      usersbullet[i].bCenter_y += usersbullet[i].bVel_y;
      usersbullet[i].bVertec_y += usersbullet[i].bVel_y;
    }
  }
  //敌人子弹
  for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++)
  {
    if(enemiesbullet[i].isExist)
    {
      LCD_DrawPic_Transparent(enemiesbullet[i].bVertec_x, enemiesbullet[i].bVertec_y, enemiesbullet[i].bPixel_x, enemiesbullet[i].bPixel_y, enemiesbullet[i].pPic);
      enemiesbullet[i].bCenter_x += enemiesbullet[i].bVel_x;
      enemiesbullet[i].bVertec_x += enemiesbullet[i].bVel_x;
      enemiesbullet[i].bCenter_y += enemiesbullet[i].bVel_y;
      enemiesbullet[i].bVertec_y += enemiesbullet[i].bVel_y;
    }
  }
  if(usersattack.isExist)
   {
      LCD_DrawPic_Transparent(usersattack.bVertec_x, usersattack.bVertec_y, usersattack.bPixel_x, usersattack.bPixel_y, usersattack.pPic);
      usersattack.bCenter_x += usersattack.bVel_x;
      usersattack.bVertec_x += usersattack.bVel_x;
      usersattack.bCenter_y += usersattack.bVel_y;
      usersattack.bVertec_y += usersattack.bVel_y;
   }
  
}
/****************生成子弹****************/
//生成玩家子弹
//TODO 制作多种子弹
void usersBullet_Create(uint8_t bulletType)
{
  static uint8_t cnt = 0;
  static uint8_t attackcnt = 0;//武器切换计数标志
  uint16_t lxlyLength;
  float currentVel_x, currentVel_y;
  float vector_UsersToEnemies[2], vector_UsersDir[2]; 
  float vector_dot;
  uint16_t UsersAndEnemies_Distance, DistanceMin = 0xFFFF;
  int8_t EnemiesNum = -1;
  
  if(usersrole->Health)
  {
    if(usersrole->Energy){usersrole->Energy -= usersweapon->wEnergy;}
    switch(bulletType)
    {
      case 0:   //武器种类是0的时候不生成子弹

      break;
      case 1:   //武器种类是1的时候生成小手枪的子弹
        while(1)
        {
          if(usersbullet[cnt].isExist == 0)
          {
            usersbullet[cnt].pPic = UsersBullet_1.pPic;
            usersbullet[cnt].bPixel_x = UsersBullet_1.bPixel_x;
            usersbullet[cnt].bPixel_y = UsersBullet_1.bPixel_y;
            usersbullet[cnt].bCenter_x = usersweapon->wGunpoint_x;
            usersbullet[cnt].bCenter_y = usersweapon->wGunpoint_y;
            usersbullet[cnt].bVertec_x = usersbullet[cnt].bCenter_x - UsersBullet_1.bPixel_x/2;
            usersbullet[cnt].bVertec_y = usersbullet[cnt].bCenter_y - UsersBullet_1.bPixel_y/2;
            usersbullet[cnt].bCrash_r = min(UsersBullet_1.bPixel_x/2, UsersBullet_1.bPixel_y/2);
            
            //角色面前的敌人子弹可以索敌
            for(uint8_t i=0; i<ENEMIES_MAX; i++)
            {
              if(enemies[i].Health)//敌人存活
              {
                //计算向量点积
                vector_calculation(vector_UsersToEnemies, usersrole->Center_x,  usersrole->Center_y, enemies[i].Center_x, enemies[i].Center_y);
                if(fabs(usersrole->Vel_x)>=1.0f || fabs(usersrole->Vel_y)>=1.0f)//角色在移动
                {
                  vector_UsersDir[0] = usersrole->Vel_x;
                  vector_UsersDir[1] = usersrole->Vel_y;
                }
                else
                {
                  vector_UsersDir[0] = usersrole->lastVel_x;
                  vector_UsersDir[1] = usersrole->lastVel_y;
                }
                vector_dot = vector_dot_product(vector_UsersToEnemies, vector_UsersDir);
                if(vector_dot >= 0.0f)//索敌并计算距离
                {
                  UsersAndEnemies_Distance = straight_distance(usersrole->Center_x, usersrole->Center_y, enemies[i].Center_x, enemies[i].Center_y);
                  if(UsersAndEnemies_Distance < DistanceMin){DistanceMin = UsersAndEnemies_Distance; EnemiesNum = i;} //记录下目前最小的距离和敌人编号
                }
              }
            }
            
            //确定子弹生成方向
            if(EnemiesNum!=-1 && DistanceMin!=0xFFFF)//视野范围内有敌人
            {
              usersbullet[cnt].bVel_x = ((float)(enemies[EnemiesNum].Center_x-usersrole->Center_x)/DistanceMin) * 15;
              usersbullet[cnt].bVel_y = ((float)(enemies[EnemiesNum].Center_y-usersrole->Center_y)/DistanceMin) * 15;
            }
            else//视野范围内没有敌人 朝着移动方向射击
            {
              lxlyLength = sqrt(usersrole->Vel_x*usersrole->Vel_x + usersrole->Vel_y*usersrole->Vel_y);
              currentVel_x = (float)(usersrole->Vel_x/lxlyLength) * 15;
              currentVel_y = (float)(usersrole->Vel_y/lxlyLength) * 15;
              if(abs(JoyStick.JoyX)<=20 && abs(JoyStick.JoyY)<=20)//站着不动   
              {
                usersbullet[cnt].bVel_x = usersrole->lastVel_x/sqrt(usersrole->lastVel_x*usersrole->lastVel_x+usersrole->lastVel_y*usersrole->lastVel_y) * 15;
                usersbullet[cnt].bVel_y = usersrole->lastVel_y/sqrt(usersrole->lastVel_x*usersrole->lastVel_x+usersrole->lastVel_y*usersrole->lastVel_y) * 15;
              }                             
              else
              {
                usersbullet[cnt].bVel_x = currentVel_x;
                usersbullet[cnt].bVel_y = currentVel_y;
              }
            }
            usersbullet[cnt].bDamage = usersweapon->wDamage;//赋予子弹伤害
            
            //最后一次干掉0速子弹
            if(fabs(usersbullet[cnt].bVel_x)<=0.5f && fabs(usersbullet[cnt].bVel_y)<=0.5f){usersbullet[cnt].isExist = 0;}
            else{usersbullet[cnt].isExist = 1;}
          
            if(cnt<USERSBULLET_MAX-1){cnt++;}
            else{cnt = 0;}
            break;
          }
          else
          {
            if(cnt<USERSBULLET_MAX-1){cnt++;}
            else{cnt = 0;}
          }
        }
        break;
      case 2:

         if(usersattack.isExist == 0)
          {
            usersattack.pPic=Knife_1.pPic;
            usersattack.bPixel_x = Knife_1.bPixel_x;
            usersattack.bPixel_y = Knife_1.bPixel_y;
            usersattack.bCenter_x = usersweapon->wGunpoint_x;
            usersattack.bCenter_y = usersweapon->wGunpoint_y;
            usersattack.bVertec_x = usersattack.bCenter_x - Knife_1.bPixel_x/2;
            usersattack.bVertec_y = usersattack.bCenter_y - Knife_1.bPixel_y/2;
            usersattack.bCrash_r = min(Knife_1.bPixel_x/2, Knife_1.bPixel_y/2);
            usersattack.bDamage = usersweapon->wDamage;//赋予伤害
            usersattack.isExist  = 1;
          }
          
         if(usersattack.isExist  == 1){attackcnt++;}
         if(attackcnt==20){usersattack.isExist  = 0;attackcnt=0;}
        break;
        
    }
  }
}

//生成敌人子弹
void enemiesBullet_Create(void)
{
  static uint8_t cnt = 0;
  uint16_t users_and_enemiesbul_dis;
  
  for(uint8_t i=0; i<ENEMIES_MAX-2; i++)//遍历敌人，boss除外
  {
    if((enemies[i].Health && usersrole->Health)&&enemies[i].Damage==1)
    {
      while(1)
      {
        if(enemiesbullet[cnt].isExist == 0)//遍历子弹
        {
          enemiesbullet[cnt].pPic = EnemiesBullet_1.pPic;
          enemiesbullet[cnt].bPixel_x = EnemiesBullet_1.bPixel_x;
          enemiesbullet[cnt].bPixel_y = EnemiesBullet_1.bPixel_y;
          enemiesbullet[cnt].bVertec_x = enemies[i].Vertec_x + enemies[i].Pixel_x/2 - EnemiesBullet_1.bPixel_x/2;
          enemiesbullet[cnt].bVertec_y = enemies[i].Vertec_y + enemies[i].Pixel_y/2 - EnemiesBullet_1.bPixel_y/2;
          enemiesbullet[cnt].bCenter_x = enemiesbullet[cnt].bVertec_x + EnemiesBullet_1.bPixel_x/2;
          enemiesbullet[cnt].bCenter_y = enemiesbullet[cnt].bVertec_y + EnemiesBullet_1.bPixel_y/2;
          enemiesbullet[cnt].bCrash_r = min(EnemiesBullet_1.bPixel_x/2, EnemiesBullet_1.bPixel_y/2);
          
          users_and_enemiesbul_dis = straight_distance(usersrole->Center_x, usersrole->Center_y, enemies[i].Center_x, enemies[i].Center_y);
          enemiesbullet[cnt].bVel_x = ((float)(usersrole->Center_x-enemies[i].Center_x)/users_and_enemiesbul_dis) * 15;//sin(theta)*V
          enemiesbullet[cnt].bVel_y = ((float)(usersrole->Center_y-enemies[i].Center_y)/users_and_enemiesbul_dis) * 15;//cos(theta)*V
          enemiesbullet[cnt].bDamage = enemies[i].Damage;
          enemiesbullet[cnt].isExist = 1;
          
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
          break;
        }
        else
        {
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
        }
      }
    }
  }
}

void enemiesTrackBullet_Create(void)
{
  static uint8_t cnt = 0;
  uint16_t users_and_enemiesbul_dis;
  for(uint8_t i=0; i<ENEMIES_MAX-2; i++)//遍历敌人，boss除外
  {
    if((enemies[i].Health && usersrole->Health)&&enemies[i].Damage==2)
    {
      while(1)
      {
        if(enemiesbullet[cnt].isExist == 0)//遍历子弹
        {
          enemiesbullet[cnt].pPic = TrackBullet.pPic;
          enemiesbullet[cnt].bPixel_x = TrackBullet.bPixel_x;
          enemiesbullet[cnt].bPixel_y = TrackBullet.bPixel_y;
          enemiesbullet[cnt].bVertec_x = enemies[i].Vertec_x + enemies[i].Pixel_x/2 - TrackBullet.bPixel_x/2;
          enemiesbullet[cnt].bVertec_y = enemies[i].Vertec_y + enemies[i].Pixel_y/2 - TrackBullet.bPixel_y/2;
          enemiesbullet[cnt].bCenter_x = enemiesbullet[cnt].bVertec_x + TrackBullet.bPixel_x/2;
          enemiesbullet[cnt].bCenter_y = enemiesbullet[cnt].bVertec_y + TrackBullet.bPixel_y/2;
          enemiesbullet[cnt].bCrash_r = min(EnemiesBullet_1.bPixel_x/2, TrackBullet.bPixel_y/2);
          
          users_and_enemiesbul_dis = straight_distance(usersrole->Center_x, usersrole->Center_y, enemies[i].Center_x, enemies[i].Center_y);
          enemiesbullet[cnt].bVel_x = ((float)(usersrole->Center_x-enemies[i].Center_x)/users_and_enemiesbul_dis) * 5;//sin(theta)*V
          enemiesbullet[cnt].bVel_y = ((float)(usersrole->Center_y-enemies[i].Center_y)/users_and_enemiesbul_dis) * 5;//cos(theta)*V
          enemiesbullet[cnt].bDamage = enemies[i].Damage;
          enemiesbullet[cnt].isExist = 1;
          
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
          break;
        }
        else
        {
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
        }
      }
    }
  }
}
//敌人的子弹追踪
void enemiesTrack(void)
{
  uint16_t users_and_Trackbul_dis;
    
  for(uint8_t i=0;i<ENEMIESBULLET_MAX;i++)
  {
     if(enemiesbullet[i].isExist == 1&&enemiesbullet[i].bDamage==2)//遍历子弹
     {
        users_and_Trackbul_dis = straight_distance(usersrole->Center_x, usersrole->Center_y, enemiesbullet[i].bCenter_x, enemiesbullet[i].bCenter_y);
        enemiesbullet[i].bVel_x = ((float)(usersrole->Center_x-enemiesbullet[i].bCenter_x)/users_and_Trackbul_dis) * 5;//sin(theta)*V
        enemiesbullet[i].bVel_y = ((float)(usersrole->Center_y-enemiesbullet[i].bCenter_y)/users_and_Trackbul_dis) * 5;//cos(theta)*V    
     }
   }
}


//生成Boss子弹
void BossBullet1_Create(void)
{
  static uint8_t cnt = 0;
  uint16_t users_and_enemiesbul_dis;
    if(enemies[ENEMIES_MAX-2].Health==0&&enemies[ENEMIES_MAX-1].Health)
    {
      while(1)
      {
        if(enemiesbullet[cnt].isExist == 0)//遍历子弹
        {
          enemiesbullet[cnt].pPic = BossBullet_1.pPic;
          enemiesbullet[cnt].bPixel_x = BossBullet_1.bPixel_x;
          enemiesbullet[cnt].bPixel_y = BossBullet_1.bPixel_y;
          enemiesbullet[cnt].bVertec_x = enemies[ENEMIES_MAX-1].Vertec_x + enemies[ENEMIES_MAX-1].Pixel_x/2 - BossBullet_1.bPixel_x/2;
          enemiesbullet[cnt].bVertec_y = enemies[ENEMIES_MAX-1].Vertec_y + enemies[ENEMIES_MAX-1].Pixel_y/2 - BossBullet_1.bPixel_y/2;
          enemiesbullet[cnt].bCenter_x = enemiesbullet[cnt].bVertec_x + BossBullet_1.bPixel_x/2;
          enemiesbullet[cnt].bCenter_y = enemiesbullet[cnt].bVertec_y + BossBullet_1.bPixel_y/2;
          enemiesbullet[cnt].bCrash_r = min(BossBullet_1.bPixel_x/2, BossBullet_1.bPixel_y/2);
          
          users_and_enemiesbul_dis = straight_distance(usersrole->Center_x, usersrole->Center_y, enemies[ENEMIES_MAX-1].Center_x, enemies[5].Center_y);
          enemiesbullet[cnt].bVel_x = ((float)(usersrole->Center_x-enemies[ENEMIES_MAX-1].Center_x)/users_and_enemiesbul_dis) * 15;//sin(theta)*V
          enemiesbullet[cnt].bVel_y = ((float)(usersrole->Center_y-enemies[ENEMIES_MAX-1].Center_y)/users_and_enemiesbul_dis) * 15;//cos(theta)*V
          enemiesbullet[cnt].bDamage = enemies[ENEMIES_MAX-1].Damage;
          enemiesbullet[cnt].isExist = 1;
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
          break;
        }
        else
        {
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
        }
      }
    }
}

//生成Boss弹幕
void BossBulletChat_Create(void)
{
  static uint8_t cnt = 0;
	float ChatNumber = 0;
    if((enemies[ENEMIES_MAX-1].Health && usersrole->Health)&&enemies[ENEMIES_MAX-2].Health>0)//boss在有护盾的时候将发射弹幕
    {
      while(1)
      {
        if(enemiesbullet[cnt].isExist == 0)//遍历子弹
        {
          enemiesbullet[cnt].pPic = BossBullet_1.pPic;
          enemiesbullet[cnt].bPixel_x = BossBullet_1.bPixel_x;
          enemiesbullet[cnt].bPixel_y = BossBullet_1.bPixel_y;
          enemiesbullet[cnt].bVertec_x = enemies[ENEMIES_MAX-1].Vertec_x + enemies[ENEMIES_MAX-1].Pixel_x/2 - BossBullet_1.bPixel_x/2;
          enemiesbullet[cnt].bVertec_y = enemies[ENEMIES_MAX-1].Vertec_y + enemies[ENEMIES_MAX-1].Pixel_y/2 - BossBullet_1.bPixel_y/2;
          enemiesbullet[cnt].bCenter_x = enemiesbullet[cnt].bVertec_x + BossBullet_1.bPixel_x/2;
          enemiesbullet[cnt].bCenter_y = enemiesbullet[cnt].bVertec_y + BossBullet_1.bPixel_y/2;
          enemiesbullet[cnt].bCrash_r = min(BossBullet_1.bPixel_x/2, BossBullet_1.bPixel_y/2);
          
          enemiesbullet[cnt].bVel_x = cos(ChatNumber*0.31415926) * 15;//速度向量
          enemiesbullet[cnt].bVel_y = sin(ChatNumber*0.31415926) * 15;
          enemiesbullet[cnt].bDamage = enemies[ENEMIES_MAX-1].Damage;
          enemiesbullet[cnt].isExist = 1;
					
					ChatNumber++;
					
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
          if(ChatNumber>19){
						break;
					}//一轮弹幕有20颗子弹
        }
        else
        {
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
        }
      }
    }
}
//生成Boss天火
void BossFire_Create(void)
{
  static uint8_t cnt = 0;
	float FireNumber = 0;
	if((BossFireFlag==0&&enemies[ENEMIES_MAX-1].Health<5)&&enemies[ENEMIES_MAX-1].Health)//boss变强时发射一次天火
    {
      while(1)
      {
        if(enemiesbullet[cnt].isExist == 0)//遍历子弹
        {
          enemiesbullet[cnt].pPic = BossFire_1.pPic;
          enemiesbullet[cnt].bPixel_x = BossFire_1.bPixel_x;
          enemiesbullet[cnt].bPixel_y = BossFire_1.bPixel_y;
          enemiesbullet[cnt].bVertec_x = 15*FireNumber;
          enemiesbullet[cnt].bVertec_y = 60;
          enemiesbullet[cnt].bCenter_x = enemiesbullet[cnt].bVertec_x + BossFire_1.bPixel_x/2;
          enemiesbullet[cnt].bCenter_y = enemiesbullet[cnt].bVertec_y + BossFire_1.bPixel_y/2;
          enemiesbullet[cnt].bCrash_r = min(BossFire_1.bPixel_x/2, BossFire_1.bPixel_y/2);
          
          enemiesbullet[cnt].bVel_x = 0;//速度向量
          enemiesbullet[cnt].bVel_y = 15;
          enemiesbullet[cnt].bDamage = enemies[ENEMIES_MAX-1].Damage;
          enemiesbullet[cnt].isExist = 1;
					
					FireNumber++;
					
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
          if(FireNumber>19){
						break;
					}//一轮弹幕有20颗子弹
        }
        else
        {
          if(cnt<ENEMIESBULLET_MAX-1){cnt++;}
          else{cnt = 0;}
        }
      }
			BossFireFlag = 1;
    }
}

//boss形态变化
void Boss_weak(void){
		if(enemies[ENEMIES_MAX-2].Health==0&&weak_flag==0){
			enemies[ENEMIES_MAX-1].pPic=Boss_mini.pPic;
			enemies[ENEMIES_MAX-1].Pixel_x=Boss_mini.Pixel_x;
			enemies[ENEMIES_MAX-1].Pixel_x=Boss_mini.Pixel_x;
			enemies[ENEMIES_MAX-1].Pixel_y=Boss_mini.Pixel_y;
			enemies[ENEMIES_MAX-1].Vertec_x=Boss_mini.Vertec_x;
			enemies[ENEMIES_MAX-1].Vertec_y=Boss_mini.Vertec_y;
			enemies[ENEMIES_MAX-1].Center_x=Boss_mini.Center_x;
			enemies[ENEMIES_MAX-1].Center_y=Boss_mini.Center_y;
			enemies[ENEMIES_MAX-1].lastCenter_x=Boss_mini.lastCenter_x;
			enemies[ENEMIES_MAX-1].lastCenter_y=Boss_mini.lastCenter_x;
			enemies[ENEMIES_MAX-1].Crash_r=Boss_mini.Crash_r;
			
			enemies[0] = Servant1;
      enemies[1] = Servant2;
			enemies[2] = Servant3;
			enemies[3] = Servant4;
			
			weak_flag=1;
		}
}
void Boss_strong(void){
		uint8_t health=0;
		health=enemies[ENEMIES_MAX-1].Health;
		if(health<5&&strong_flag==0){
			enemies[ENEMIES_MAX-1].pPic=Boss1.pPic;
			enemies[ENEMIES_MAX-1].Pixel_x=Boss1.Pixel_x;
			enemies[ENEMIES_MAX-1].Pixel_x=Boss1.Pixel_x;
			enemies[ENEMIES_MAX-1].Pixel_y=Boss1.Pixel_y;
			enemies[ENEMIES_MAX-1].Vertec_x=Boss1.Vertec_x;
			enemies[ENEMIES_MAX-1].Vertec_y=Boss1.Vertec_y;
			enemies[ENEMIES_MAX-1].Center_x=Boss1.Center_x;
			enemies[ENEMIES_MAX-1].Center_y=Boss1.Center_y;
			enemies[ENEMIES_MAX-1].lastCenter_x=Boss1.lastCenter_x;
			enemies[ENEMIES_MAX-1].lastCenter_y=Boss1.lastCenter_x;
			enemies[ENEMIES_MAX-1].Crash_r=Boss1.Crash_r;
			enemies[ENEMIES_MAX-1].Health=health;//Boss形态改变
			
			strong_flag=1;
		}
}
//TODO 生成敌人的追踪子弹

//TODO 生成敌人其他类型的子弹
/****************绘制地图****************/
//画状态栏
void statusBarUpdate(void)
{
  uint8_t Health_Pixel_x, Shield_Pixel_x, Energy_Pixel_x, Power_Pixel_x;
  uint8_t i = 0;
  
  //绘制状态栏
  LCD_DrawPic_Opaque(StatusBar1.Vertec_x, StatusBar1.Vertec_y, StatusBar1.Pixel_x, StatusBar1.Pixel_y, StatusBar1.pPic);
  LCD_DrawPic_Opaque(StatusBar2.Vertec_x, StatusBar2.Vertec_y, StatusBar2.Pixel_x, StatusBar2.Pixel_y, StatusBar2.pPic);
  LCD_DrawPic_Opaque(StatusBar3.Vertec_x, StatusBar3.Vertec_y, StatusBar3.Pixel_x, StatusBar3.Pixel_y, StatusBar3.pPic);
  LCD_DrawPic_Opaque(StatusBar4.Vertec_x, StatusBar4.Vertec_y, StatusBar4.Pixel_x, StatusBar4.Pixel_y, StatusBar4.pPic);
  
  //更新玩家血量
  for(i = 0; i<usersrole->Health; i++)
  {
    if(i == 0){Health_Pixel_x = roundNum((float)(StatusBar1.DrawPiexl_x) / usersrole->MaxHealth);}
    else{Health_Pixel_x = roundNum((float)(StatusBar1.DrawPiexl_x-(StatusBar1.DrawVertec_x-StatusBar1.ConstDrawVertec_x)) / (usersrole->MaxHealth-i));}
    
    LCD_FillWindowColor(StatusBar1.DrawVertec_x, StatusBar1.DrawVertec_y, Health_Pixel_x, StatusBar1.DrawPiexl_y, RED);
    StatusBar1.DrawVertec_x += Health_Pixel_x;
    if(i != usersrole->Health-1)
    {
      LCD_FillWindowColor(StatusBar1.DrawVertec_x, StatusBar1.DrawVertec_y, StatusBar1.WhitePiexl_x, StatusBar1.WhitePiexl_y, WHITE);
      StatusBar1.DrawVertec_x += StatusBar1.WhitePiexl_x;
    }
  }
  StatusBar1.WhitePiexl_x = StatusBar1.DrawPiexl_x - (StatusBar1.DrawVertec_x-StatusBar1.ConstDrawVertec_x);
  LCD_FillWindowColor(StatusBar1.DrawVertec_x, StatusBar1.DrawVertec_y, StatusBar1.WhitePiexl_x, StatusBar1.DrawPiexl_y, WHITE);
  StatusBar1.DrawVertec_x = StatusBar1.ConstDrawVertec_x;
  StatusBar1.WhitePiexl_x = 1;
  
  //更新玩家护盾
  for(i = 0; i<usersrole->Shield; i++)
  {
    if(i == 0){Shield_Pixel_x = roundNum((float)(StatusBar2.DrawPiexl_x) / usersrole->MaxShield);}
    else{Shield_Pixel_x = roundNum((float)(StatusBar2.DrawPiexl_x-(StatusBar2.DrawVertec_x-StatusBar2.ConstDrawVertec_x)) / (usersrole->MaxShield-i));}
    
    LCD_FillWindowColor(StatusBar2.DrawVertec_x, StatusBar2.DrawVertec_y, Shield_Pixel_x, StatusBar2.DrawPiexl_y, GREY);
    StatusBar2.DrawVertec_x += Shield_Pixel_x;
    if(i != usersrole->Shield-1)
    {
      LCD_FillWindowColor(StatusBar2.DrawVertec_x, StatusBar2.DrawVertec_y, StatusBar2.WhitePiexl_x, StatusBar2.WhitePiexl_y, WHITE);
      StatusBar2.DrawVertec_x += StatusBar2.WhitePiexl_x;
    }
  }
  StatusBar2.WhitePiexl_x = StatusBar2.DrawPiexl_x - (StatusBar2.DrawVertec_x-StatusBar2.ConstDrawVertec_x);
  LCD_FillWindowColor(StatusBar2.DrawVertec_x, StatusBar2.DrawVertec_y, StatusBar2.WhitePiexl_x, StatusBar2.DrawPiexl_y, WHITE);
  StatusBar2.DrawVertec_x = StatusBar2.ConstDrawVertec_x;
  StatusBar2.WhitePiexl_x = 1;
  
  //更新玩家蓝量
  Energy_Pixel_x = StatusBar3.DrawPiexl_x / usersrole->MaxEnergy;
  for(i = 0; i<usersrole->Energy; i++)
  {
    LCD_FillWindowColor(StatusBar3.DrawVertec_x, StatusBar3.DrawVertec_y, Energy_Pixel_x, StatusBar3.DrawPiexl_y, BLUE);
    StatusBar3.DrawVertec_x += Energy_Pixel_x;
  }
  StatusBar3.WhitePiexl_x = StatusBar3.DrawPiexl_x - i*Energy_Pixel_x;
  LCD_FillWindowColor(StatusBar3.DrawVertec_x, StatusBar3.DrawVertec_y, StatusBar3.WhitePiexl_x, StatusBar3.DrawPiexl_y, WHITE);
  StatusBar3.DrawVertec_x = StatusBar3.ConstDrawVertec_x;
  StatusBar3.WhitePiexl_x = 1;
  
  //更新玩家技能冷却
  Power_Pixel_x = StatusBar4.DrawPiexl_x / usersrole->MaxPower;
  for(i = 0; i<usersrole->Power; i++)
  {
    LCD_FillWindowColor(StatusBar4.DrawVertec_x, StatusBar4.DrawVertec_y, Power_Pixel_x, StatusBar4.DrawPiexl_y, YELLOW);
    StatusBar4.DrawVertec_x += Power_Pixel_x;
  }
  StatusBar4.WhitePiexl_x = StatusBar4.DrawPiexl_x - i*Power_Pixel_x;
  LCD_FillWindowColor(StatusBar4.DrawVertec_x, StatusBar4.DrawVertec_y, StatusBar4.WhitePiexl_x, StatusBar4.DrawPiexl_y, WHITE);
  StatusBar4.DrawVertec_x = StatusBar4.ConstDrawVertec_x;
  StatusBar4.WhitePiexl_x = 1;
}

//画边界墙壁
void mapBoundaryUpdate(void)
{
  uint8_t i = 0;
  //绘制顶边
  Wall.Vertec_x = 0; //起点在状态栏下方
  Wall.Vertec_y = StatusBar1.Pixel_y;
  for(i=0; i<20; i++)
  {
    LCD_DrawPic_Opaque(Wall.Vertec_x, Wall.Vertec_y, Wall.Pixel_x, Wall.Pixel_y, Wall.pPic);
    Wall.Vertec_x += Wall.Pixel_x;
  }
  //绘制左边
  Wall.Vertec_x = 0;
  Wall.Vertec_y = StatusBar1.Pixel_y + Wall.SquarePixel_y;
  for(i=0; i<11; i++)
  {
    LCD_DrawPic_Opaque(Wall.Vertec_x, Wall.Vertec_y, Wall.SquarePixel_x, Wall.SquarePixel_y, Wall.pPic);
    Wall.Vertec_y += Wall.SquarePixel_y;
  }
  LCD_DrawPic_Opaque(Wall.Vertec_x, Wall.Vertec_y, Wall.Pixel_x, Wall.Pixel_y, Wall.pPic);
  //绘制右边
  Wall.Vertec_x = Wall.Pixel_x * 19;
  Wall.Vertec_y = StatusBar1.Pixel_y + Wall.SquarePixel_y;
  for(i=0; i<11; i++)
  {
    LCD_DrawPic_Opaque(Wall.Vertec_x, Wall.Vertec_y, Wall.SquarePixel_x, Wall.SquarePixel_y, Wall.pPic);
    Wall.Vertec_y += Wall.SquarePixel_y;
  }
  LCD_DrawPic_Opaque(Wall.Vertec_x, Wall.Vertec_y, Wall.Pixel_x, Wall.Pixel_y, Wall.pPic);
  //绘制底边
  Wall.Vertec_x = Wall.SquarePixel_x;
  Wall.Vertec_y = StatusBar1.Pixel_y + Wall.SquarePixel_y * 12;
  for(i=0; i<18; i++)
  {
    LCD_DrawPic_Opaque(Wall.Vertec_x, Wall.Vertec_y, Wall.Pixel_x, Wall.Pixel_y, Wall.pPic);
    Wall.Vertec_x += Wall.Pixel_x;
  }
}

//画地板
void mapFloorUpdate(void)
{
  uint8_t i = 0, j = 0;
  
  Floor1.Vertec_x = Wall.Pixel_x;
  Floor1.Vertec_y = StatusBar1.Pixel_y + Wall.Pixel_y;
  for(i=0; i<18; i++)
  {
    Floor1.Vertec_y = StatusBar1.Pixel_y + Wall.Pixel_y; 
    for(j=0; j<11; j++)
    {
      if(Floor1.pPic == FloorPic.pFloor2)
      {
        Floor1.pPic = FloorPic.pFloor1;
        if(j == 10){LCD_DrawPic_Opaque(Floor1.Vertec_x, Floor1.Vertec_y, Floor1.Pixel_x, Floor1.Pixel_y/2, Floor1.pPic);}
        else{LCD_DrawPic_Opaque(Floor1.Vertec_x, Floor1.Vertec_y, Floor1.Pixel_x, Floor1.Pixel_y, Floor1.pPic);}
        Floor1.Vertec_y += Floor1.Pixel_y;
      }
      else if(Floor1.pPic == FloorPic.pFloor1)
      {
        Floor1.pPic = FloorPic.pFloor2;
        if(j == 10){LCD_DrawPic_Opaque(Floor1.Vertec_x, Floor1.Vertec_y, Floor1.Pixel_x, Floor1.Pixel_y/2, Floor1.pPic);}
        else{LCD_DrawPic_Opaque(Floor1.Vertec_x, Floor1.Vertec_y, Floor1.Pixel_x, Floor1.Pixel_y, Floor1.pPic);}
        Floor1.Vertec_y += Floor1.Pixel_y;
      }
    }
    Floor1.Vertec_x += Floor1.Pixel_x;
  }
}

//画不可破坏地图障碍
void mapBarrierUpdate(void)
{
  uint8_t i = 0;
  for(i=0; i< MAPBARRIER_MAX; i++)
  {
    if(mapBarrier[i].isCreated == 0){break;}
    LCD_DrawPic_Transparent(mapBarrier[i].Vertec_x, mapBarrier[i].Vertec_y, mapBarrier[i].Pixel_x, mapBarrier[i].Pixel_y, mapBarrier[i].pPic);
  }
}
//画可破坏地图障碍
void mapDesBarrierUpdate(void)
{
  uint8_t i = 0;
  for(i=0; i< MAPDESBARRIER_MAX; i++)
  {
    if(mapDesBarrier[i].isCreated == 1 && mapDesBarrier[i].isExist == 1)
		{
			LCD_DrawPic_Transparent(mapDesBarrier[i].Vertec_x, mapDesBarrier[i].Vertec_y, mapDesBarrier[i].Pixel_x, mapDesBarrier[i].Pixel_y, mapDesBarrier[i].pPic);
		}
	}
}

/****************绘制游戏界面****************/
//游戏关卡初始化
void gameLevel_Init(uint8_t *levelNum)
{
  switch(*levelNum)//这里主要初始化地图和敌人配置
  {
    case 0:
    //清除怪物
			ClearEnemies();
		//角色绑定
      usersrole = &Knight;
      enemies[0] = Servant1;
      enemies[1] = Enemy2;
		
      //角色位置的初始化
      usersrole->Vertec_x = 30;
      usersrole->Vertec_y = 100;
      usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;
      usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
      
      //武器位置初始化
      usersweapon->wVertec_x = usersrole->Center_x;
      usersweapon->wVertec_y = usersrole->Center_y;
      usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
      usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;
    
      //清除所有的子弹
      for(uint8_t i=0; i<USERSBULLET_MAX; i++){usersbullet[i].isExist = 0;}
      for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++){enemiesbullet[i].isExist = 0;}
      usersattack.isExist = 0;
      //填充角色全部的蓝量
      usersrole->Energy = usersrole->MaxEnergy;
      
      //地图初始化
			for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapBarrier[i].isCreated = 0;}
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++)
			{
				mapDesBarrier[i].isCreated = 0;
				mapDesBarrier[i].isExist = 0;
			}
			

			gameMap00_Create();
      break;
		
		case 1:
      //角色绑定
      usersrole = &Knight;
      enemies[0] = Servant1;
    
      //角色位置的初始化
      usersrole->Vertec_x = 30;
      usersrole->Vertec_y = 100;
      usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;
      usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
      
      //武器位置初始化
      usersweapon->wVertec_x = usersrole->Center_x;
      usersweapon->wVertec_y = usersrole->Center_y;
      usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
      usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;
    
      //清除所有的子弹
      for(uint8_t i=0; i<USERSBULLET_MAX; i++){usersbullet[i].isExist = 0;}
      for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++){enemiesbullet[i].isExist = 0;}
      usersattack.isExist = 0;
      //填充角色全部的蓝量
      usersrole->Energy = usersrole->MaxEnergy;
      
      //地图初始化
			for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapBarrier[i].isCreated = 0;}
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++)
			{
				mapDesBarrier[i].isCreated = 0;
				mapDesBarrier[i].isExist = 0;
			}
			
			gameMap01_Create();			

      break;
    
    case 2:
			//角色绑定
      usersrole = &Knight;
      enemies[0] = Enemy3;
      enemies[1] = Enemy4;
    
      //角色位置的初始化
      usersrole->Vertec_x = 30;
      usersrole->Vertec_y = 100;
      usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;
      usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
      
      //武器位置初始化
      usersweapon->wVertec_x = usersrole->Center_x;
      usersweapon->wVertec_y = usersrole->Center_y;
      usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
      usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;
		
			//清除所有的子弹
      for(uint8_t i=0; i<USERSBULLET_MAX; i++){usersbullet[i].isExist = 0;}
      for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++){enemiesbullet[i].isExist = 0;}
      
      //填充角色全部的蓝量
      usersrole->Energy = usersrole->MaxEnergy;
      
      //地图初始化
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapBarrier[i].isCreated = 0;}
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapDesBarrier[i].isCreated = 0;mapDesBarrier[i].isExist = 0;}
			
			gameMap02_Create();
      break;
			
		case 3:
      //角色绑定
      usersrole = &Knight;
      enemies[0] = Servant1;
      enemies[1] = Enemy2;
    
      //角色位置的初始化
      usersrole->Vertec_x = 30;
      usersrole->Vertec_y = 100;
      usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;
      usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
      
      //武器位置初始化
      usersweapon->wVertec_x = usersrole->Center_x;
      usersweapon->wVertec_y = usersrole->Center_y;
      usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
      usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;
    
      //清除所有的子弹
      for(uint8_t i=0; i<USERSBULLET_MAX; i++){usersbullet[i].isExist = 0;}
      for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++){enemiesbullet[i].isExist = 0;}
      usersattack.isExist = 0;
      //填充角色全部的蓝量
      usersrole->Energy = usersrole->MaxEnergy;
      
      //地图初始化
			for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapBarrier[i].isCreated = 0;}
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++)
			{
				mapDesBarrier[i].isCreated = 0;
				mapDesBarrier[i].isExist = 0;
			}
			
			gameMap01_Create();			
      break;
    
    case 4:
			//角色绑定
      usersrole = &Knight;
      enemies[0] = Enemy3;
      enemies[1] = Enemy4;
    
      //角色位置的初始化
      usersrole->Vertec_x = 30;
      usersrole->Vertec_y = 100;
      usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;
      usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
      
      //武器位置初始化
      usersweapon->wVertec_x = usersrole->Center_x;
      usersweapon->wVertec_y = usersrole->Center_y;
      usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
      usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;
		
			//清除所有的子弹
      for(uint8_t i=0; i<USERSBULLET_MAX; i++){usersbullet[i].isExist = 0;}
      for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++){enemiesbullet[i].isExist = 0;}
      
      //填充角色全部的蓝量
      usersrole->Energy = usersrole->MaxEnergy;
      
      //地图初始化
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapBarrier[i].isCreated = 0;}
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapDesBarrier[i].isCreated = 0;mapDesBarrier[i].isExist = 0;}
			
			gameMap02_Create();
      break;
			
		case 5:
			//角色绑定
      usersrole = &Knight;
		
			//Boss状态初始化
		  enemies[ENEMIES_MAX-2] = Boss_shield;
			enemies[ENEMIES_MAX-1] = Boss1;
			BossFireFlag=0;
			weak_flag=0;
			strong_flag=0;
    
      //角色位置的初始化
      usersrole->Vertec_x = 30;
      usersrole->Vertec_y = 100;
      usersrole->Center_x = usersrole->Vertec_x + usersrole->Pixel_x/2;
      usersrole->Center_y = usersrole->Vertec_y + usersrole->Pixel_y/2;
      
      //武器位置初始化
      usersweapon->wVertec_x = usersrole->Center_x;
      usersweapon->wVertec_y = usersrole->Center_y;
      usersweapon->wGunpoint_x = usersweapon->wVertec_x + usersweapon->wPixel_x;
      usersweapon->wGunpoint_y = usersweapon->wVertec_y + 5;
		
			//清除所有的子弹
      for(uint8_t i=0; i<USERSBULLET_MAX; i++){usersbullet[i].isExist = 0;}
      for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++){enemiesbullet[i].isExist = 0;}
      
      //填充角色全部的蓝量
      usersrole->Energy = usersrole->MaxEnergy;
      
      //地图初始化
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapBarrier[i].isCreated = 0;}
      for(uint8_t i=0; i<MAPBARRIER_MAX; i++){mapDesBarrier[i].isCreated = 0;mapDesBarrier[i].isExist = 0;}
			
			gameMap03_Create();
      break;
  }
}

//绘制开始界面
void startGameSurfaceCreate(void)
{
  static uint8_t clear_Flag = 1;
  if(clear_Flag)
  {
    LCD_SetColors(RED, BLACK);
    ILI9341_Clear(0, 0, LCD_X_LENGTH, LCD_Y_LENGTH);
    LCD_SetColors(RED, WHITE);
    clear_Flag = 0;
  }
  LCD_DrawPic_Transparent(59, 100, 200, 43, Surface_Pic.startPic);
}

//绘制传送界面
void TansferSurfaceCreate(void)
{
  static uint8_t trans_cnt = 0;
  if(tansflag == 1) {ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);tansflag = 0;}
  switch(trans_cnt)

  {
    case 0:
      LCD_DrawPic_Transparent(135,88,50,64,Tansfer_Gate.Tans0);
      trans_cnt++;
      break;
    
    case 2:
      LCD_DrawPic_Transparent(135,88,50,64,Tansfer_Gate.Tans1);
      trans_cnt++;
      break;
    
    case 4:
      LCD_DrawPic_Transparent(135,88,50,64,Tansfer_Gate.Tans2);
      trans_cnt++;
      break;
    
    case 6:
      LCD_DrawPic_Transparent(135,88,50,64,Tansfer_Gate.Tans3);
      trans_cnt = 0;
      gameTansTime++;
      break;
    
    default:
      trans_cnt++;
      break;
  }
}
//绘制结束界面
void endGameSurfaceCreate(void)
{
  //清除所有的子弹
  for(uint8_t i=0; i<USERSBULLET_MAX; i++){usersbullet[i].isExist = 0;}
  for(uint8_t i=0; i<ENEMIESBULLET_MAX; i++){enemiesbullet[i].isExist = 0;}
  usersattack.isExist = 0;
  
  if(usersrole->Health)
  {
    LCD_DrawPic_Transparent(119, 30, 80, 40, Surface_Pic.victoryText);
    LCD_DrawPic_Transparent(109, 100, 100, 110, Surface_Pic.victoryPic);
  }
  else
  {
    LCD_DrawPic_Transparent(79, 30, 160, 40, Surface_Pic.failText);
    LCD_DrawPic_Transparent(109, 110, 100, 96, Surface_Pic.failPic);
  }
}

/****************闯关结束条件判断****************/
//1为闯关成功 0为未成功
uint8_t endGameLevelCheck(void)
{
  uint8_t enemiesSurvivalNum = 0;
  for(uint8_t i=0; i<ENEMIES_MAX; i++)
  {
    if(enemies[i].Health != 0){enemiesSurvivalNum++;}
  }
  if(enemiesSurvivalNum == 0){return 1;}
  else{return 0;}
}

void ClearEnemies(void){
	for(uint8_t i=0;i<ENEMIES_MAX;i++){
		enemies[i].Health=0;
	}
}
